<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 *  Entrance File of the Control class.
 *
 * @Author:    Bruce(sanshi)<sanshi0815@tom.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: admin_user.class.php 2008-12-11 07:06:14 $
 * @Show Note: 后台用户管理模块
 */
Class admin_user extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;
		
		//组权限
		private $admin_group_permission = array();
		/**
		 *	Construct this module class.
		*/
		public function admin_user()
		{}
		public function set()
		{
			$this->TheAdmin();
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("admin_user");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			$admin_group_temp =Factory::getDB("admin_group")->getAll("admin_group_state=1");
			$admin_group_arr = array();
			$admin_group_permission = array();
			foreach ($admin_group_temp as $v)
			{
				$admin_group_arr[$v['admin_group_id']] = $v['admin_group_title'];
				$this->admin_group_permission[$v['admin_group_id']] = $v['admin_group_permission'];
			}
			
			$user_state_arr = array(1=>'正常',0=>'异常');
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('admin_group_arr',$admin_group_arr);
			$this->smarty->assign('user_state_arr',$user_state_arr);
			$this->smarty->assign('menus',$menu);

			$this->smarty->assign("main_menus",$this->get_user_menus());
			$this->tplFile = "admin_user.tpl";

		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->admin_user();
		}
		
		public function _display()
	    {
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
				$col = "admin_user_id";
				$where = 1;
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				$this->smarty->assign('arr',$arr);
				//默认方法
				
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						$permission_arr = $this->get_permission_arr();
						//使用模版，展示提交界面
						
						$this->smarty->assign('permission_arr',$permission_arr);
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_admin_user($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"添加 id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=admin_user');
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$admin_user_id = isset($_REQUEST['admin_user_id']) ? intval(trim($_REQUEST['admin_user_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($admin_user_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=admin_user');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						
						$admin_permission_arr = unserialize($result['admin_user_permission']);
						$this->smarty->assign('admin_permission_arr',$admin_permission_arr);
						
						$permission_arr = $this->get_permission_arr();
						
						
						$this->smarty->assign('permission_arr',$permission_arr);
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "admin_user_id=$admin_user_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_admin_user($temp,$where);
						$error_no = 0 > $update ?  $update :'-1502';
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=admin_user');
				}
		}
		
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$admin_user_id = isset($_REQUEST['admin_user_id']) ? intval(trim($_REQUEST['admin_user_id'])) : 0 ;
				$where = "admin_user_id=$admin_user_id";
				$del=$this->db->delete_admin_user($where);
				$error_no = 0 > $del ?  $del :'-1503';
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				alertMsg($error_no, 'index.php?c=admin_user');
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['admin_user_id'] = $_POST['admin_user_id'];
				$temp['admin_user_pwd'] = 
							$_POST['admin_user_pwd'] != $_POST['reg_admin_user_pwd'] ?
									md5($_POST['admin_user_pwd']) :  $_POST['reg_admin_user_pwd'];
			} else {

				$temp['admin_user_id'] = "";
				$temp['admin_user_pwd'] = md5($_POST['admin_user_pwd']);
				$check_no = $this->db->getSumNumber($this->db->tableName,"admin_user_name='{$_POST['admin_user_name']}'");
				if($check_no!=0)
				{
					alertMsg('-2002');
					exit();
				}
			}
			//补充其他的字段
			$temp['admin_user_name'] = $_POST['admin_user_name'];
			$temp['admin_user_mail'] = $_POST['admin_user_mail'];
			$temp['admin_user_mobile'] = $_POST['admin_user_mobile'];
			$temp['admin_user_time'] = time();
			$temp['admin_user_state'] = intval($_POST['admin_user_state']);
			$temp['admin_group_id'] = intval($_POST['admin_group_id']);
			$temp['admin_user_permission_type'] = intval($_POST['admin_user_permission_type']);
			if($temp['admin_user_permission_type']==0)
			{
				$temp['admin_user_permission'] = $this->admin_group_permission[$temp['admin_group_id']];
			}else {
				$permission_arr = array();
				foreach ((array)$_POST['admin_user_permission'] AS $k=>$v)
				{
					$permission_arr[$k] = array_sum($v);
				}
				$temp['admin_user_permission'] = serialize($permission_arr);
			}
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);		
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'后台用户管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
}

?>